// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const db = uniCloud.database();
const dbCmd = db.command;
module.exports = {
	add({
		user_id,
		name,
		tel,
		province,
		city,
		area,
		areaCode,
		detail,
		is_default
	}) {
		return db.collection('address').add({
			user_id,
			name,
			tel,
			province,
			city,
			area,
			areaCode,
			detail,
			is_default,
			address: province + city + area + detail
		})
	},
	list(user_id) {
		return db.collection('address').where({
			user_id
		}).get()
	},
	async setDefault(user_id,_id){
			// console.log(user_id,_id);
		// 1.首先把所有该用户地址修改成非默认状态
		let update = await db.collection('address').where({
			user_id:dbCmd.eq(user_id)
		}).update({
			is_default:false
		})
		// 2.把传入的地址设置为默认地址
		let def = await db.collection('address').doc(_id).update({
			is_default:true
		})
		return def
	},
	get(_id){
		return db.collection('address').doc(_id).get()
	}
}
